<?php
namespace app\beautiful\controller\miniapp\staff;
use app\common\logic\DataLogic;
use app\beautiful\model\mall\GoodsCategoryModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\member\MemberModel;
use app\BaseController;
use app\beautiful\logic\PartnerLogic;

class Main extends BaseController{
    public function getStaffInfo()
    {
        $info=[
            'face'=>$this->request->member->face,
            'name'=>$this->request->staff->name,
            'mobile'=>$this->request->staff->mobile,
            'apply_vip'=>in_array(1,$this->request->staff->add_value_jurisdiction) ? 1 : 0,
            'write_off_jurisdiction'=>$this->request->staff->write_off_jurisdiction,
            'add_value_jurisdiction'=>$this->request->staff->add_value_jurisdiction,
        ];
        return $this->success(['info'=>$info]);
    }

    public function getSearchMemeberId()
    {
        $mobile = $this->request->param('mobile');
        if(empty($mobile) || !is_mobile($mobile)){
            return $this->error('请输入手机号');
        }

        if (null === $member = MemberModel::where([
                ['mobile','=',$mobile],
                ['shop_id','=',$this->request->shopId],
                ['status','=',1],
                ['is_delete','=',0]
            ])->find()) {
            return $this->error('会员不存在');
        }
        return $this->success(['member_id'=>$member->member_id]);
    }

    public function scanCheckMember()
    {
        $member_id = $this->request->param('member_id/d',0);
        if(null === $member = MemberModel::find($member_id)) return $this->error('用户不存在');
        if($member->shop_id != $this->request->shopId) return $this->error('用户不存在');
        if($member->status != 1) return $this->error('用户不存在');
        if($member->is_delete !=0) return $this->error('用户不存在');

        $code = $this->request->param('code');
        if(empty($code)) return $this->error('会员码不正确');
        if($member->code != $code) return $this->error('会员码不正确');

        return $this->success([]);
    }

    public function applyVip()
    {
        //判断权限
        if (!in_array(1, $this->request->staff->add_value_jurisdiction)) {
            return $this->error('您没有办理会员的权限');
        }
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $birthday=$this->request->param('birthday');

        if(empty($nick_name) || mb_strlen($nick_name) >32) return $this->error('请输入昵称且昵称长度不能多余32');
        if(empty($mobile) || !is_mobile($mobile)) return $this->error('请输入手机号');
        if(empty($birthday)) return $this->error('请选择生日');
        if(false === strtotime($birthday." 00:00:00")) return $this->error('请选择正确的生日');
        $birthday_arr=explode('-',$birthday);
        if(count($birthday_arr) !=3) return $this->error('请选择正确的生日');
        $birthday_year=$birthday_arr[0]; $birthday_month=$birthday_arr[1]; $birthday_day=$birthday_arr[2];

        //判断手机号
        if(null !== MemberModel::where([
                ['shop_id','=',$this->request->shopId],
                ['mobile','=',$mobile],
                ['status','=',1],
                ['is_delete','=',0]
            ])->find()){
            return $this->error('该手机号已经存在了');
        }

        $member = new MemberModel();
        $member->shop_id =$this->request->shopId;
        $member->nick_name=$nick_name;
        $member->mobile=$mobile;
        $member->birthday_year=$birthday_year;
        $member->birthday_month=$birthday_month;
        $member->birthday_day=$birthday_day;
        $rst = $member->save();
        if($rst) {
            $partnerLogic = new PartnerLogic($this->request->shopId);
            $partnerLogic->memberPartner($mobile,$nick_name);
            $partnerLogic->bindPartner($member->member_id,$this->request->memberId,true);
        }
        return $this->success(['member_id'=>$member->member_id]);
    }

//    public function getGoodsCategory()
//    {
//        $category= GoodsCategoryModel::where([
//            ['shop_id','=',$this->request->shopId],
//            ['is_delete','=',0],
//            ['pid','=',0]
//        ])->field('category_id,name')->select()->toArray();
//        $datas=$this->getGoodsCategoryData($category,[]);
//        return $this->success(['datas'=>$datas]);
//    }
//
//    protected function getGoodsCategoryData($datas,$return1)
//    {
//        if(!empty($datas)){
//            $return2=[];
//            foreach ($datas as $k => $v){
//                $category= GoodsCategoryModel::where([
//                    ['shop_id','=',$this->request->shopId],
//                    ['is_delete','=',0],
//                    ['pid','=',$v['category_id']]
//                ])->field('category_id,name')->select()->toArray();
//
//                if(empty($category)){
//                    $return2[]=$v;
//                }else{
//                    $return2=$this->getGoodsCategoryData($category,$return2);
//                }
//            }
//        }
//        return array_merge($return1,$return2);
//    }

    public function chooseGoods()
    {
        $name=$this->request->param('name');
        $type=$this->request->param('type','look');
        $sort=$this->request->param('sort/d',0);
        $category_id = $this->request->param('category_id/d',0);
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
            ['is_open_yu','=',0],
            ['status','=',1],
            ['quota','>',0]
        ];
        if(!empty($name)) $where[]=['name','=',$name];
        if(!empty($category_id)) $where[]=['category_id','=',$category_id];
        $goods_model= new GoodsModel();
//        $goods_model = $goods_model->with(['sku'=>function($query){
//            $query->where([
//                ['shop_id','=',$this->request->shopId],
//                ['is_delete','=',0]
//            ]);
//        }]);

        $order=['sort'=>'desc','update_time'=>'desc','goods_id'=>'desc'];
        if($sort == 1 )  $order=['take_count'=>'desc','sort'=>'desc','update_time'=>'desc','goods_id'=>'desc'];
        if($sort ==-1 )  $order=['take_count'=>'asc','sort'=>'desc','update_time'=>'desc','goods_id'=>'desc'];
        $append=['add_time_format'];
        $field="goods_id,cover_img,name,vip_price,original_price,take_count,add_time";

        $datas=$goods_model->where($where)
            ->order($order)
            ->field($field)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select()
            ->append($append)
            ->toArray();

        $return['datas']=$datas;
        return $this->success($return);
    }


}